package pe.com.csi.pm.regexiv;

import pe.com.csi.pm.regexiv.util.UConstante;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.Toast;

public class TabFotosAlcantarilla extends Activity {

	private ImageButton btnCamera;
	private ImageButton btnDelete;
	private Cursor mCursor;
	private Gallery mGallery;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_fotos_alcantarilla);
		System.out.println("OnCreate TabFotos");
		btnCamera = (ImageButton) findViewById(R.id.btnCamera);
		btnDelete = (ImageButton) findViewById(R.id.btnDelete);
		mGallery = (Gallery) findViewById(R.id.tblImagenes);
		cargarEventos();
		displayGallery();

	}

	private void cargarEventos() {
		try {
			btnCamera.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
					i.putExtra(MediaStore.EXTRA_OUTPUT, Media.EXTERNAL_CONTENT_URI.toString());
					startActivityForResult(i, UConstante.CAMERA_ACTIVITY);
				}
			});

			btnDelete.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					ContentResolver cr = getContentResolver();
					String selection = MediaColumns.TITLE + " = \"" + UConstante.OUR_TITLE_ALCANTARILLA + "\"";
					cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, null);
					displayGallery();
				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void displayGallery() {
		try {
			Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
			String[] projection = { BaseColumns._ID, MediaColumns.TITLE, MediaStore.Images.ImageColumns.LATITUDE, MediaStore.Images.ImageColumns.LONGITUDE, MediaColumns.DISPLAY_NAME, };
			String selection = MediaColumns.TITLE + " = \"" + UConstante.OUR_TITLE_ALCANTARILLA + "\"";
			mCursor = this.managedQuery(uri, projection, selection, null, null);
			if (mCursor != null) {
				mCursor.moveToFirst();
				ImageAdapter adapter = new ImageAdapter(mCursor, this);
				mGallery.setAdapter(adapter);
			} else
				showToast(this, "No hay imagenes.");
		} catch (Exception e) {
			showToast(this, e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == UConstante.CAMERA_ACTIVITY) {
			try {
				ContentResolver cr = getContentResolver();
				final String[] p1 = new String[] { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_TAKEN };
				Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC LIMIT 1 ");
				if (c1 != null) {
					if (c1.moveToFirst()) {
						int id = c1.getInt(0);
						ContentValues values = new ContentValues();
						values.put(MediaColumns.TITLE, UConstante.OUR_TITLE_ALCANTARILLA);
						String where = MediaStore.Images.ImageColumns._ID + " =" + id;
						cr.update(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values, where, null);
					}
					c1.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			displayGallery();
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		System.out.println("OnPause TabFotos");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		displayGallery();
	}

	private void showToast(Context mContext, String text) {
		Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
	}
}
